﻿using DidaCode.Workflow.Logs;
using DidaCode.Workflow.Models.Parameters;
using DidaCode.Workflow.Models.Results;
using DidaCode.Workflow.Models;
using DidaCode.Workflow.NodeAttribute;
using DidaCode.Workflow.NodeBase;
using System.Text.Encodings.Web;
using System.Text.Json;


namespace DidaCode.Workflow.Nodes
{

    [NodeType(Node.HotelSearchLog)]
    public class HotelSearchLog : NodeBase<HotelSearchParameter, HotelSearchResult>
    {
        public HotelSearchLog(string switchConfig) : base(switchConfig)
        {
        }

        protected override async Task<NodeResult<HotelSearchResult>> ExecuteNodeAsync(ExecutionContext<HotelSearchParameter, HotelSearchResult> context)
        {
            HotelSearchResult searchResult = new HotelSearchResult { };
            if (ExecutionLogger.TryGetResult<string>(Node.HotelSearch, out var HotelSearchNodeResult))
            {
                searchResult.HotelName = HotelSearchNodeResult;
            }
            if (ExecutionLogger.TryGetResult<string>(Node.RateAdjust, out var rateAdjustNodeResult))
            {
                searchResult.RateAdjustResult = rateAdjustNodeResult;
            }
            if (ExecutionLogger.TryGetResult<List<string>>(Node.RatePlan, out var RatePlanNodeResult))
            {
                searchResult.RatePlanResult = RatePlanNodeResult;
            }
            var json = JsonSerializer.Serialize(searchResult, new JsonSerializerOptions { WriteIndented = true, Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping });
            await Console.Out.WriteLineAsync($"发送日志{json}");
            return NodeResult<HotelSearchResult>.SuccessNode(searchResult);
        }
    }
}
